package com.yxhl.zoume.core.tripsmgmt.ui.fragment.home;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.main.ui.MainActivity;
import com.yxhl.zoume.core.tripsmgmt.ui.adapter.HomeTripsPagerAdapter;
import com.yxhl.zoume.utils.LOG;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * created by sugarya
 * 行程模块主页
 */
public class TripsHomeFragment extends BaseFragment implements ViewPager.OnPageChangeListener {

    private static final String TAG = "TripsHomeFragment";

    // widgets
    @BindView(R.id.tab_trips_mgmt_home)
    TabLayout mTabLayout;

    @BindView(R.id.pager_trips_mgmt_home)
    ViewPager mViewPager;

    // arguments
    private List<BaseFragment> mFragmentList;

    private List<String> mTitleList;

    private HomeTripsPagerAdapter mHomeTripsPagerAdapter;


    public TripsHomeFragment() {
        // Required empty public constructor
    }

    public static TripsHomeFragment newInstance() {
        TripsHomeFragment fragment = new TripsHomeFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LOG.e(TAG, "onCreate savedInstanceState=" + savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        LOG.e(TAG, "onCreateView savedInstanceState=" + savedInstanceState);
        View view = super.onCreateView(inflater, container, savedInstanceState);

        if (mFragmentList == null) {
            mFragmentList = new ArrayList<>();
        } else {
            mFragmentList.clear();
        }

        CurrentTripsFragment currentTripsFragment = CurrentTripsFragment.newInstance();
        HistoryTripsFragment historyTripsFragment = HistoryTripsFragment.newInstance();
        mFragmentList.add(currentTripsFragment);
        mFragmentList.add(historyTripsFragment);

        if (mTitleList == null) {
            mTitleList = new ArrayList<>();
        } else {
            mTitleList.clear();
        }
        mTitleList.add(getString(R.string.tab_current_trips));
        mTitleList.add(getString(R.string.tab_history_trips));

        mHomeTripsPagerAdapter = new HomeTripsPagerAdapter(getChildFragmentManager(), mFragmentList, mTitleList);
        mViewPager.setAdapter(mHomeTripsPagerAdapter);

        TabLayout.Tab currentTripTab = mTabLayout.newTab().setText(getString(R.string.tab_current_trips));
        TabLayout.Tab historyTripTab = mTabLayout.newTab().setText(getString(R.string.tab_history_trips));
        mTabLayout.addTab(currentTripTab, true);
        mTabLayout.addTab(historyTripTab, false);
        mTabLayout.setupWithViewPager(mViewPager);


        mViewPager.addOnPageChangeListener(this);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                CurrentTripsFragment.sNeedLoadData = true;
                break;
            case 1:
                CurrentTripsFragment.sNeedLoadData = false;
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    @Override
    protected void initializeInjector() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LOG.e(TAG, "onDestroyView");
    }

    @Override
    public void onDestroy() {
        LOG.e(TAG, "onDestroy");
        //初始化状态重置
        CurrentTripsFragment.sNeedLoadData = true;
        super.onDestroy();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_trips_mgmt_home;
    }

    @Override
    protected void setupToolBar() {
        if (getActivity() instanceof MainActivity) {
            ((MainActivity) getActivity()).setToolbarTitle(getString(R.string.main_tab_name_trip));
            ((MainActivity) mActivity).setToolbarVisible(true);
        }
    }

}
